import { story, xstorys, ystorys } from './story'
import { wearg, thing } from './thing';
const playerData = storage.getGroupStorage<PlayerData>('playerdata')!
export interface PlayerData {
    lv: number
    exp: number
    item: Array<any>
    bagM: number
    wear_item: Array<wearg>
    jinen: Array<thing>
    hp: number
    maxHp: number
    talk: number
    jxd: number
    main_weapon: string
    deputy_weapon: string
    attack: number
    defense: number
    sore: number
    story: string
}
export const defaultData: PlayerData = {
    lv: 1,
    exp: 0,
    item: [],
    bagM: 30,
    wear_item: [],
    jinen: [],
    hp: 100,
    maxHp: 100,
    talk: 100000,
    jxd: 0,
    main_weapon: '暂无',
    deputy_weapon: '暂无',
    attack: 3,
    defense: 3,
    sore: 500,
    story: '故事背景',
}
export class Player {
    data!: PlayerData
    entity: GamePlayerEntity
    constructor(entity: GamePlayerEntity) {
        this.entity = entity
        this.data = defaultData
        this.loadData()
    }
    async bag() {
        const main = await this.entity.player.dialog({
            type: GameDialogType.SELECT,
            title: this.entity.player.name,
            content: `行动值: ${this.data.talk}/100000\n血量: ${this.data.hp}/${this.data.maxHp}\n信用点: ${this.data.sore}\n攻击力: ${this.data.attack}\n佩戴武器(主手): ${this.data.main_weapon}\n等级: ${this.data.lv}\n绩效点: ${this.data.jxd}\n升到${this.data.lv + 1}级还需${this.data.lv * 300 + 500}点经验\n当前剧情进度：${this.data.story}`,
            options: ['保存', '背包', '技能',/*'玩法'*/'删档']
        })
        if (!main || main === null) return
        if (main) {
            if (main.value == '保存') {
                this.upData()
                await this.entity.player.dialog({
                    type: GameDialogType.TEXT,
                    title: this.entity.player.name,
                    content: `保存成功！`,
                })
            }
            if (main.value == '背包') {
                let t: Array<string> = []
                for (let a of this.data.item) {
                    t.push(a.name)
                }
                const bag = await this.entity.player.dialog({
                    type: GameDialogType.SELECT,
                    title: this.entity.player.name,
                    content: `背包空间${this.data.item.length}/${this.data.bagM}`,
                    options: t.length == 0 ? ['暂无'] : t
                })
                if (!bag || bag === null || bag!.value == '暂无') return
                if (bag) {
                    let t!: thing
                    for (let a of this.data.item) {
                        if (a.name == bag.value) {
                            t = a
                            break
                        }
                    }
                    if (t.attribute == '装备') {
                        const bag2 = await this.entity.player.dialog({
                            type: GameDialogType.SELECT,
                            title: bag.value,
                            content: t.content,
                            options: ['穿戴', '取消']
                        })
                        if (!bag2 || bag2 === null) return
                        if (bag2!.value == '穿戴') return this.wear(t.name, t.mesh!, t.bodyPart!, t.orientation!, t.scale!, t.offset!)
                    } else {
                        await this.entity.player.dialog({
                            type: GameDialogType.TEXT,
                            title: bag.value,
                            content: t.content,
                        })
                    }
                }
            }
            if (main.value == '技能') {
                let con: string = '你有以下技能：'
                for (let a of this.data.jinen) {
                    con = `${con}\n${a}`
                }
                await this.entity.player.dialog({
                    type: GameDialogType.TEXT,
                    title: '技能',
                    content: con
                })
            }
            if (main.value == '删档') {
                await this.deleteData()
            }
        }
    }
    async deleteData() {
        if (!this.entity.player.userId) return;
        await playerData.remove(this.entity.player.userId)
    }
    async loadData() {
        if (!this.entity.player.userId) return
        let data = await playerData.get(this.entity.player.userId);
        if (!data) return;
        //Object.entries(data.value).forEach(([key, value]) => console.warn(key + ':' + value))
        this.data = {
            ...defaultData,
            ...data.value
        }

    }
    async upData() {
        if (!this.entity.player.userId) return;
        await playerData.set(this.entity.player.userId, {
            ...this.data
        })

    }
    async interact(interId?: string) {
        if (!(this.data.story.includes(interId!)) && interId) return;
        let content: Array<string> = []
        for (let [key, value] of Object.entries(xstorys)) {
            if (key == this.data.story) { content = value; break };
        }
        for (let a of content) {
            let con = await story(this.data.story, a, this.entity)
            if (typeof con !== 'boolean') {
                if (a.includes('任务')) {
                    console.log(a.split(':')[1])
                    this.data.story = a.split(':')[1]
                }
                continue
            }
            else if (con == true) {
                if (a.includes('任务')) {
                    console.log(a.split(':')[1])
                    this.data.story = a.split(':')[1]
                }
                continue
            } else {
                let content2: Array<string> = []
                for (let [key, value] of Object.entries(ystorys)) {
                    if (key == this.data.story) { content2 = value; break };
                }
                for (let b of content2) {
                    await story(this.data.story, b, this.entity)
                }
                this.entity.player.kick()
            }
        }
    }
    async wear(name: string, mesh: GameModelAssets, bodyPart: GameBodyPart, orientation: GameQuaternion, scale: GameVector3, offset: GameVector3) {
        let t: number | undefined = undefined;
        for (var [index, value] of this.data.wear_item.entries()) {
            if (this.data.wear_item.length == 0) break;
            if (value.bodyPart == bodyPart) {
                const th = await this.entity.player.dialog({
                    type: GameDialogType.SELECT,
                    content: '你确定要更换' + value.name + '吗?',
                    options: ['确定', '取消']
                })
                if (th?.value == '确定') {
                    const s = this.entity.player.wearables(value.bodyPart);
                    this.entity.player.removeWearable(s[0]);
                    t = index
                    break
                } else {
                    return;
                }
            }
        }
        t == undefined ? this.data.wear_item.push({ name: name, mesh: mesh, bodyPart: bodyPart, orientation: orientation, scale: scale, offset: offset }) : this.data.wear_item[t] = { name: name, mesh: mesh, bodyPart: bodyPart, orientation: orientation, scale: scale, offset: offset }
        this.entity.player.addWearable({
            mesh: mesh,
            bodyPart: bodyPart,
            orientation: orientation,
            scale: scale,
            offset: offset
        })
        await this.entity.player.dialog({
            type: GameDialogType.TEXT,
            content: '穿戴成功！',
        })

    }

    remove(name: string): void {
        for (var [index, value] of this.data.wear_item.entries()) {
            if (value.name == name) {
                this.data.wear_item.splice(index, 1)
            }
        }
    }


}